package ${bean.packageName};

import com.framework.controller.BaseController;
import com.framework.util.*;
import ${bean.importPackage}.model.${bean.className};
import ${bean.importPackage}.service.${bean.className}Service;
import flexjson.JSONSerializer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
* Created with generator tools.
* User: ${bean.createUser}
* DateTime:${bean.createTime}
*/
@Controller
public class ${bean.className}Controller extends BaseController<${bean.className}> {

private static final String path_index = "${bean.lowerClassName}/index";
private static final String path_grid = "${bean.lowerClassName}/grid";
private static final String path_input = "${bean.lowerClassName}/input";
private static final String path_view = "${bean.lowerClassName}/view";

@Resource
private ${bean.className}Service ${bean.lowerClassName}Service;

@RequestMapping
public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
System.out.println("index method ....");
return path_index;
}

@RequestMapping
public String grid(Model model, HttpServletRequest request, HttpServletResponse response) {
System.out.println("grid method ....");
return path_grid;
}

@ResponseBody
@RequestMapping
protected void getGridData(HttpServletRequest request, HttpServletResponse response) throws Exception {
Pagination pagination = RequestVarsUtil.getPagination(request);
/*  List<${bean.className}> entitys = this.groupService.find(pagination);
int total = ${bean.lowerClassName}Service.getTotal();*/
List
<Condition> conditions = this.getSearchConditions(request);
    List<${bean.className}> entitys = this.${bean.lowerClassName}Service.listAndSort(pagination, conditions);
    int total = this.${bean.lowerClassName}Service.getTotal(conditions);
    ResultDto
    <List
    <${bean.className}>> resultDto = new ResultDto
    <List
    <${bean.className}>>();
    resultDto.setRows(entitys);
    resultDto.setTotal(total);
    resultDto.setSuccess(true);
    System.out.println(" resultDto.toString() = " + resultDto.toString());
    super.writeToClient(response, resultDto.toString());
    }

    @RequestMapping
    public String edit(String id, Model model, HttpServletRequest request, HttpServletResponse response) {
    System.out.println("edit method ....");
${bean.className} ${bean.lowerClassName} = this.${bean.lowerClassName}Service.get(id);
    //model.addAttribute("${bean.lowerClassName}", ${bean.lowerClassName});
    return path_input;
    }

    @RequestMapping
    public String add(String id, Model model, HttpServletRequest request, HttpServletResponse response) {
    System.out.println("add method ....");
    //${bean.className} ${bean.lowerClassName} = new ${bean.className}();
    //model.addAttribute("${bean.lowerClassName}", ${bean.lowerClassName});
    return path_input;
    }

    @RequestMapping
    public String view(String id, Model model, HttpServletRequest request, HttpServletResponse response) {
    System.out.println("view method ....");
${bean.className} ${bean.lowerClassName} = this.${bean.lowerClassName}Service.get(id);
    //${bean.className} ${bean.lowerClassName} = new ${bean.className}();
    //model.addAttribute("${bean.lowerClassName}", ${bean.lowerClassName});
    return path_view;
    }

    @RequestMapping
    @DateTimeFormat
    public void save(${bean.className} ${bean.lowerClassName}, HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    System.out.println("save method ....");
    MessageDto messageDto = new MessageDto();
    try {
    if (!StringUtils.isBlank(${bean.lowerClassName}.getId())) {
    this.${bean.lowerClassName}Service.update(${bean.lowerClassName});
    super.setMessageDto(messageDto, MessageDefined.update_success_info);
    } else {
    this.${bean.lowerClassName}Service.save(${bean.lowerClassName});
    super.setMessageDto(messageDto, MessageDefined.save_success_info);
    }
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    }
    super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void delete(String id, Model model, HttpServletRequest request, HttpServletResponse response) throws
    IOException {
    MessageDto messageDto = new MessageDto();
    try {
    id = super.transforIds(response, id);
    this.${bean.lowerClassName}Service.delete(id);
    super.setMessageDto(messageDto, MessageDefined.del_success_info);
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    }
    super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void deleteMore(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
    MessageDto messageDto = new MessageDto();
    try {
    ids = super.transforIds(response, ids);
    Integer count = this.${bean.lowerClassName}Service.delete(ids);
    super.setMessageDto(messageDto, MessageDefined.del_success_info);
    } catch (Exception e) {
    messageDto.setSuccess(false);
    super.setMessageDto(messageDto, MessageDefined.sys_error_info);
    }
    super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    public void getKeyValueDtos(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException,
    IOException, SQLException {
    List<${bean.className}> entitys = this.${bean.lowerClassName}Service.findAll();
    JSONSerializer jsonSerializer = new JSONSerializer();
    String results = jsonSerializer.include("id", "name").exclude("*").serialize(entitys);
    System.out.println("results = " + results);
    super.writeToClient(response, results);
    }
    }
